package org.finesys.chat.core.base.embedding.store.pgvector;

import org.finesys.common.core.util.ValidationUtil;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class MetadataColumnDefinition {

    private final String fullDefinition;
    private final String name;
    private final String type;

    public MetadataColumnDefinition(String fullDefinition, String name, String type) {
        this.fullDefinition = fullDefinition;
        this.name = name;
        this.type = type;
    }

    public static MetadataColumnDefinition from(String sqlDefinition) {
        String fullDefinition = ValidationUtil.ensureNotNull(sqlDefinition, "Metadata column definition");
        List<String> parts = Arrays.stream(sqlDefinition.split(" ")).filter(s -> !s.isEmpty()).collect(Collectors.toList());
        if (parts.size() < 2) {
            throw new IllegalArgumentException("Definition format should be: column type [ NULL | NOT NULL ] [ UNIQUE ] [ DEFAULT value ]");
        } else {
            String name = parts.get(0);
            String type = parts.get(1).toLowerCase();
            return new MetadataColumnDefinition(fullDefinition, name, type);
        }
    }

    public String getFullDefinition() {
        return fullDefinition;
    }

    public String getName() {
        return name;
    }

    public String getType() {
        return type;
    }
}
